package com.dattplace.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ColorModel;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageUtil {
	/** 
     * 几种常见的图片格式 
     */
    public static String IMAGE_TYPE_GIF = "gif";// 图形交换格式 
    public static String IMAGE_TYPE_JPG = "jpg";// 联合照片专家组 
    public static String IMAGE_TYPE_JPEG = "jpeg";// 联合照片专家组 
    public static String IMAGE_TYPE_BMP = "bmp";// 英文Bitmap（位图）的简写，它是Windows操作系统中的标准图像文件格式 
    public static String IMAGE_TYPE_PNG = "png";// 可移植网络图形 
    public static String IMAGE_TYPE_PSD = "psd";// Photoshop的专用格式Photoshop 
      
    /** 
     * 缩放图像（按比例缩放） 
     * @param srcImageFile 源图像文件地址 
     * @param result 缩放后的图像地址 
     * @param scale 缩放比例 
     * @param flag 缩放选择:true 放大; false 缩小; 
     */
    public final static void scale(String srcImageFile, String result, 
            int scale, boolean flag) { 
        try { 
            BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件 
            int width = src.getWidth(); // 得到源图宽 
            int height = src.getHeight(); // 得到源图长 
            if (flag) {// 放大 
                width = width * scale; 
                height = height * scale; 
            } else {// 缩小 
                width = width / scale; 
                height = height / scale; 
            } 
            Image image = src.getScaledInstance(width, height, 
                    Image.SCALE_DEFAULT); 
            BufferedImage tag = new BufferedImage(width, height, 
                    BufferedImage.TYPE_INT_RGB); 
            Graphics g = tag.getGraphics(); 
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图 
            g.dispose(); 
            ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
      
    /** 
     * 缩放图像（按高度和宽度缩放） 
     * @param srcImageFile 源图像文件地址 
     * @param height 缩放后的高度 
     * @param width 缩放后的宽度 
     * @param bb 比例不对时是否需要补白：true为补白; false为不补白; 
     */
    public final static byte[] scale2(InputStream srcImageFile, int height, int width, boolean bb) { 
        try { 
            double ratio = 0.0; // 缩放比例 
            BufferedImage bi = ImageIO.read(srcImageFile); 
            @SuppressWarnings("static-access")
            Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH); 
            // 计算比例 
            if ((bi.getHeight() > height) || (bi.getWidth() > width)) { 
                if (bi.getHeight() > bi.getWidth()) { 
                    ratio = (new Integer(height)).doubleValue() 
                            / bi.getHeight(); 
                } else { 
                    ratio = (new Integer(width)).doubleValue() / bi.getWidth(); 
                } 
                AffineTransformOp op = new AffineTransformOp(AffineTransform 
                        .getScaleInstance(ratio, ratio), null); 
                itemp = op.filter(bi, null); 
            } 
            if (bb) {//补白 
                BufferedImage image = new BufferedImage(width, height, 
                        BufferedImage.TYPE_INT_RGB); 
                Graphics2D g = image.createGraphics(); 
                g.setColor(Color.white); 
                g.fillRect(0, 0, width, height); 
                if (width == itemp.getWidth(null)) 
                    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, 
                            itemp.getWidth(null), itemp.getHeight(null), 
                            Color.white, null); 
                else
                    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, 
                            itemp.getWidth(null), itemp.getHeight(null), 
                            Color.white, null); 
                g.dispose(); 
                itemp = image; 
            } 
            //ImageIO.write((BufferedImage) itemp, "JPEG", new File(result)); 
            ByteArrayOutputStream out = new ByteArrayOutputStream(); 
            ImageIO.write((BufferedImage) itemp, "jpg", out);  
            return out.toByteArray(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
            return null; 
        } 
    } 
      
    /** 
     * 缩放图像（按高度和宽度缩放） 
     * @param srcImageFile 源图像文件地址 
     * @param result 缩放后的图像地址 
     * @param height 缩放后的高度 
     * @param width 缩放后的宽度 
     * @param bb 比例不对时是否需要补白：true为补白; false为不补白; 
     */
    @SuppressWarnings("static-access")
    public final static boolean scale2(String srcImageFile, String result, int height, int width, boolean bb) { 
        try { 
            double ratio = 0.0; // 缩放比例 
            File f = new File(srcImageFile); 
            BufferedImage bi = ImageIO.read(f); 
            int imageWidth = bi.getWidth();
            int imageHeight = bi.getHeight();
          
            if(imageWidth <= width) return false;
            float imgWidth = imageWidth;
            height = (int) (width/imgWidth*imageHeight); //按宽度压缩比计算高度
            Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH); 
            // 计算比例 
            if ((bi.getHeight() > height) || (bi.getWidth() > width)) { 
                if (bi.getHeight() > bi.getWidth()) { 
                    ratio = (new Integer(height)).doubleValue() 
                            / bi.getHeight(); 
                } else { 
                    ratio = (new Integer(width)).doubleValue() / bi.getWidth(); 
                } 
                AffineTransformOp op = new AffineTransformOp(AffineTransform 
                        .getScaleInstance(ratio, ratio), null); 
                itemp = op.filter(bi, null); 
            } 
            if (bb) {//补白 
                BufferedImage image = new BufferedImage(width, height, 
                        BufferedImage.TYPE_INT_RGB); 
                Graphics2D g = image.createGraphics(); 
                g.setColor(Color.white); 
                g.fillRect(0, 0, width, height); 
                if (width == itemp.getWidth(null)) 
                    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, 
                            itemp.getWidth(null), itemp.getHeight(null), 
                            Color.white, null); 
                else
                    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, 
                            itemp.getWidth(null), itemp.getHeight(null), 
                            Color.white, null); 
                g.dispose(); 
                itemp = image; 
            } 
            ImageIO.write((BufferedImage) itemp, "JPG", new File(result)); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return true;
    } 
     
      
    /** 
     * 图像切割(按指定起点坐标和宽高切割) 
     * @param srcImageFile 源图像地址 
     * @param result 切片后的图像地址 
     * @param x 目标切片起点坐标X 
     * @param y 目标切片起点坐标Y 
     * @param width 目标切片宽度 
     * @param height 目标切片高度 
     */
    public final static void cut(String srcImageFile,String result,
            int x, int y, int width, int height,String imgType) { 
//        try { 
//            // 读取源图像 
//            BufferedImage bi = ImageIO.read(new File(srcImageFile)); 
//            int srcWidth = bi.getHeight(); // 源图宽度 
//            int srcHeight = bi.getWidth(); // 源图高度 
//            if (srcWidth > 0 && srcHeight > 0) { 
//                Image image = bi.getScaledInstance(srcWidth, srcHeight, 
//                        Image.SCALE_DEFAULT); 
//                // 四个参数分别为图像起点坐标和宽高 
//                // 即: CropImageFilter(int x,int y,int width,int height) 
//                ImageFilter cropFilter = new CropImageFilter(x, y, width, height); 
//                Image img = Toolkit.getDefaultToolkit().createImage( 
//                        new FilteredImageSource(image.getSource(), 
//                                cropFilter)); 
//                BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
//                Graphics g = tag.getGraphics(); 
//              g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图 
//                g.dispose(); 
//                // 输出为文件 
//                ImageIO.write(tag, "JPEG", new File(result)); 
//            } 
//        } catch (Exception e) { 
//            e.printStackTrace(); 
//        } 
        
        
        try { 
            // 读取源图像 
            BufferedImage bufImg = ImageIO.read(new File(srcImageFile)); 
            //切割图像
            BufferedImage subImg = bufImg.getSubimage(x, y, width, height);
            ImageIO.write(subImg,imgType,new File(result));
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
      
    
    /** 
     * 图像切割(按指定起点坐标和宽高切割) 
     * @param srcImageFile 源图像地址 
     * @param result 切片后的图像地址 
     * @param x 目标切片起点坐标X 
     * @param y 目标切片起点坐标Y 
     * @param width 目标切片宽度 
     * @param height 目标切片高度 
     */
    public final static void cut5(String srcImageFile,String result,String imgType) { 
        try { 
            // 读取源图像 
            BufferedImage bufImg = ImageIO.read(new File(srcImageFile)); 

            int srcWidth = bufImg.getHeight(); // 源图宽度 
            int srcHeight = bufImg.getWidth(); // 源图高度 
            int x=0;
            int y=0;
            int width =0;
            int height=0;;
            
            if(srcWidth > srcHeight){  // 如果宽大于高
                 x =0 ;
                 y =srcWidth/2 -srcHeight/2;
                 width =srcHeight;
                 height =srcHeight;
            }else{
                 y =0 ;
                 x =srcHeight/2 -srcWidth/2;
                 width =srcWidth;
                 height =srcWidth;
            }
            //切割图像
            BufferedImage subImg = bufImg.getSubimage(x, y, width, height);
            ImageIO.write(subImg,imgType,new File(result));
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    
    
    /** 
     * 图像切割（指定切片的行数和列数） 
     * @param srcImageFile 源图像地址 
     * @param descDir 切片目标文件夹 
     * @param rows 目标切片行数。默认2，必须是范围 [1, 20] 之内 
     * @param cols 目标切片列数。默认2，必须是范围 [1, 20] 之内 
     */
    public final static void cut2(String srcImageFile, String descDir, 
            int rows, int cols) { 
        try { 
            if(rows<=0||rows>20) rows = 2; // 切片行数 
            if(cols<=0||cols>20) cols = 2; // 切片列数 
            // 读取源图像 
            BufferedImage bi = ImageIO.read(new File(srcImageFile)); 
            int srcWidth = bi.getHeight(); // 源图宽度 
            int srcHeight = bi.getWidth(); // 源图高度 
            if (srcWidth > 0 && srcHeight > 0) { 
                Image img; 
                ImageFilter cropFilter; 
                Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); 
                int destWidth = srcWidth; // 每张切片的宽度 
                int destHeight = srcHeight; // 每张切片的高度 
                // 计算切片的宽度和高度 
                if (srcWidth % cols == 0) { 
                    destWidth = srcWidth / cols; 
                } else { 
                    destWidth = (int) Math.floor(srcWidth / cols) + 1; 
                } 
                if (srcHeight % rows == 0) { 
                    destHeight = srcHeight / rows; 
                } else { 
                    destHeight = (int) Math.floor(srcWidth / rows) + 1; 
                } 
                // 循环建立切片 
                // 改进的想法:是否可用多线程加快切割速度 
                for (int i = 0; i < rows; i++) { 
                    for (int j = 0; j < cols; j++) { 
                        // 四个参数分别为图像起点坐标和宽高 
                        // 即: CropImageFilter(int x,int y,int width,int height) 
                        cropFilter = new CropImageFilter(j * destWidth, i * destHeight, 
                                destWidth, destHeight); 
                        img = Toolkit.getDefaultToolkit().createImage( 
                                new FilteredImageSource(image.getSource(), 
                                        cropFilter)); 
                        BufferedImage tag = new BufferedImage(destWidth, 
                                destHeight, BufferedImage.TYPE_INT_RGB); 
                        Graphics g = tag.getGraphics(); 
                        g.drawImage(img, 0, 0, null); // 绘制缩小后的图 
                        g.dispose(); 
                        // 输出为文件 
                        ImageIO.write(tag, "JPEG", new File(descDir 
                                + "_r" + i + "_c" + j + ".jpg")); 
                    } 
                } 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    /** 
     * 图像切割（指定切片的宽度和高度） 
     * @param srcImageFile 源图像地址 
     * @param descDir 切片目标文件夹 
     * @param destWidth 目标切片宽度。默认200 
     * @param destHeight 目标切片高度。默认150 
     */
    public final static void cut3(String srcImageFile, String descDir, 
            int destWidth, int destHeight) { 
        try { 
            if(destWidth<=0) destWidth = 200; // 切片宽度 
            if(destHeight<=0) destHeight = 150; // 切片高度 
            // 读取源图像 
            BufferedImage bi = ImageIO.read(new File(srcImageFile)); 
            int srcWidth = bi.getHeight(); // 源图宽度 
            int srcHeight = bi.getWidth(); // 源图高度 
            if (srcWidth > destWidth && srcHeight > destHeight) { 
                Image img; 
                ImageFilter cropFilter; 
                Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); 
                int cols = 0; // 切片横向数量 
                int rows = 0; // 切片纵向数量 
                // 计算切片的横向和纵向数量 
                if (srcWidth % destWidth == 0) { 
                    cols = srcWidth / destWidth; 
                } else { 
                    cols = (int) Math.floor(srcWidth / destWidth) + 1; 
                } 
                if (srcHeight % destHeight == 0) { 
                    rows = srcHeight / destHeight; 
                } else { 
                    rows = (int) Math.floor(srcHeight / destHeight) + 1; 
                } 
                // 循环建立切片 
                // 改进的想法:是否可用多线程加快切割速度 
                for (int i = 0; i < rows; i++) { 
                    for (int j = 0; j < cols; j++) { 
                        // 四个参数分别为图像起点坐标和宽高 
                        // 即: CropImageFilter(int x,int y,int width,int height) 
                        cropFilter = new CropImageFilter(j * destWidth, i * destHeight, 
                                destWidth, destHeight); 
                        img = Toolkit.getDefaultToolkit().createImage( 
                                new FilteredImageSource(image.getSource(), 
                                        cropFilter)); 
                        BufferedImage tag = new BufferedImage(destWidth, 
                                destHeight, BufferedImage.TYPE_INT_RGB); 
                        Graphics g = tag.getGraphics(); 
                        g.drawImage(img, 0, 0, null); // 绘制缩小后的图 
                        g.dispose(); 
                        // 输出为文件 
                        ImageIO.write(tag, "JPEG", new File(descDir 
                                + "_r" + i + "_c" + j + ".jpg")); 
                    } 
                } 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    /** 
     * 图像类型转换：GIF->JPG、GIF->PNG、PNG->JPG、PNG->GIF(X)、BMP->PNG 
     * @param srcImageFile 源图像地址 
     * @param formatName 包含格式非正式名称的 String：如JPG、JPEG、GIF等 
     * @param destImageFile 目标图像地址 
     */
	public final static void convert(String srcImageFile, String formatName,
			String destImageFile) {
		try {
			File f = new File(srcImageFile);
			f.canRead();
			BufferedImage sourceImage = ImageIO.read(f);
			BufferedImage destImage = new BufferedImage(sourceImage.getWidth(),
					sourceImage.getHeight(), BufferedImage.TYPE_INT_RGB);
			destImage.createGraphics().drawImage(sourceImage, 0, 0,
					Color.WHITE, null);

			ImageIO.write(destImage, formatName, new File(destImageFile));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

    /** 
     * 彩色转为黑白  
     * @param srcImageFile 源图像地址 
     * @param destImageFile 目标图像地址 
     */
    public final static void gray(String srcImageFile, String destImageFile) { 
        try { 
            BufferedImage src = ImageIO.read(new File(srcImageFile)); 
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); 
            ColorConvertOp op = new ColorConvertOp(cs, null); 
            src = op.filter(src, null); 
            ImageIO.write(src, "JPEG", new File(destImageFile)); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
    /** 
     * 给图片添加文字水印 
     * @param pressText 水印文字 
     * @param srcImageFile 源图像地址 
     * @param destImageFile 目标图像地址 
     * @param fontName 水印的字体名称 
     * @param fontStyle 水印的字体样式 
     * @param color 水印的字体颜色 
     * @param fontSize 水印的字体大小 
     * @param x 修正值 
     * @param y 修正值 
     * @param alpha 透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字 
     */
    public final static void pressText(String pressText, 
            String srcImageFile, String destImageFile, String fontName, 
            int fontStyle, Color color, int fontSize,int x, 
            int y, float alpha) { 
        try { 
            File img = new File(srcImageFile); 
            Image src = ImageIO.read(img); 
            int width = src.getWidth(null); 
            int height = src.getHeight(null); 
            BufferedImage image = new BufferedImage(width, height, 
                    BufferedImage.TYPE_INT_RGB); 
            Graphics2D g = image.createGraphics(); 
            g.drawImage(src, 0, 0, width, height, null); 
            g.setColor(color); 
            g.setFont(new Font(fontName, fontStyle, fontSize)); 
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 
                    alpha)); 
            // 在指定坐标绘制水印文字 
            g.drawString(pressText, (width - (getLength(pressText) * fontSize)) 
                    / 2 + x, (height - fontSize) / 2 + y); 
            g.dispose(); 
            ImageIO.write((BufferedImage) image, "JPEG", new File(destImageFile));// 输出到文件流 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    /** 
     * 给图片添加文字水印 
     * @param pressText 水印文字 
     * @param srcImageFile 源图像地址 
     * @param destImageFile 目标图像地址 
     * @param fontName 字体名称 
     * @param fontStyle 字体样式 
     * @param color 字体颜色 
     * @param fontSize 字体大小 
     * @param x 修正值 
     * @param y 修正值 
     * @param alpha 透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字 
     */
    public final static void pressText2(String pressText, String srcImageFile,String destImageFile, 
            String fontName, int fontStyle, Color color, int fontSize, int x, 
            int y, float alpha) { 
        try { 
            File img = new File(srcImageFile); 
            Image src = ImageIO.read(img); 
            int width = src.getWidth(null); 
            int height = src.getHeight(null); 
            BufferedImage image = new BufferedImage(width, height, 
                    BufferedImage.TYPE_INT_RGB); 
            Graphics2D g = image.createGraphics(); 
            g.drawImage(src, 0, 0, width, height, null); 
            g.setColor(color); 
            g.setFont(new Font(fontName, fontStyle, fontSize)); 
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 
                    alpha)); 
            // 在指定坐标绘制水印文字 
            g.drawString(pressText, (width - (getLength(pressText) * fontSize)) 
                    / 2 + x, (height - fontSize) / 2 + y); 
            g.dispose(); 
            ImageIO.write((BufferedImage) image, "JPEG", new File(destImageFile)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    /** 
     * 给图片添加图片水印 
     * @param pressImg 水印图片 
     * @param srcImageFile 源图像地址 
     * @param destImageFile 目标图像地址 
     * @param x 修正值。 默认在中间 
     * @param y 修正值。 默认在中间 
     * @param alpha 透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字 
     */
    public final static void pressImage(String pressImg, String srcImageFile,String destImageFile, 
            int x, int y, float alpha) { 
        try { 
            File img = new File(srcImageFile); 
            Image src = ImageIO.read(img); 
            int wideth = src.getWidth(null); 
            int height = src.getHeight(null); 
            BufferedImage image = new BufferedImage(wideth, height, 
                    BufferedImage.TYPE_INT_RGB); 
            Graphics2D g = image.createGraphics(); 
            g.drawImage(src, 0, 0, wideth, height, null); 
            // 水印文件 
            Image src_biao = ImageIO.read(new File(pressImg)); 
            int wideth_biao = src_biao.getWidth(null); 
            int height_biao = src_biao.getHeight(null); 
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 
                    alpha)); 
            g.drawImage(src_biao, (wideth - wideth_biao) / 2, 
                    (height - height_biao) / 2, wideth_biao, height_biao, null); 
            // 水印文件结束 
            g.dispose(); 
            ImageIO.write((BufferedImage) image,  "JPEG", new File(destImageFile)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    /** 
     * 计算text的长度（一个中文算两个字符） 
     * @param text 
     * @return 
     */
    public final static int getLength(String text) { 
        int length = 0; 
        for (int i = 0; i < text.length(); i++) { 
            if (new String(text.charAt(i) + "").getBytes().length > 1) { 
                length += 2; 
            } else { 
                length += 1; 
            } 
        } 
        return length / 2; 
    } 
    
    public static BufferedImage toBufferedImage(Image image) {  

    	     if (image instanceof BufferedImage) {  

    	        return (BufferedImage)image;  

    	      }  

    	   

    	     // This code ensures that all the pixels in the image are loaded  

    	      image = new ImageIcon(image).getImage();  

    	    

    	     // Determine if the image has transparent pixels; for this method's  

    	     // implementation, see e661 Determining If an Image Has Transparent Pixels  

    	    //boolean hasAlpha = hasAlpha(image);  

    	    

    	     // Create a buffered image with a format that's compatible with the screen  

    	      BufferedImage bimage = null;  

    	      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  

    	     try {  

    	         // Determine the type of transparency of the new buffered image  

    	         int transparency = Transparency.OPAQUE;  

    	        /* if (hasAlpha) {  

    	          transparency = Transparency.BITMASK;  

    	          }*/  

    	    

    	         // Create the buffered image  

    	          GraphicsDevice gs = ge.getDefaultScreenDevice();  

    	          GraphicsConfiguration gc = gs.getDefaultConfiguration();  

    	          bimage = gc.createCompatibleImage(  

    	         image.getWidth(null), image.getHeight(null), transparency);  

    	      } catch (HeadlessException e) {  

    	         // The system does not have a screen  

    	      }  

    	    

    	    if (bimage == null) {  

    	         // Create a buffered image using the default color model  

    	         int type = BufferedImage.TYPE_INT_RGB;  

    	         //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang  

    	         /*if (hasAlpha) {  

    	          type = BufferedImage.TYPE_INT_ARGB;  

    	         }*/ 

    	         bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);  

    	      }  

    	    

    	     // Copy image to buffered image  

    	      Graphics g = bimage.createGraphics();  

       

    	     // Paint the image onto the buffered image  

    	      g.drawImage(image, 0, 0, null);  

    	      g.dispose();  

    	   

    	     return bimage;  

    	 } 

    /**  
     * 实现图像的等比缩�?  
     * @param source  
     * @param targetW  
     * @param targetH  
     * @return  
     */
    private static BufferedImage resize(BufferedImage source, int targetW,
            int targetH) {
        // targetW，targetH分别表示目标长和�?   
        int type = source.getType();
        BufferedImage target = null;
        double sx = (double) targetW / source.getWidth();
        double sy = (double) targetH / source.getHeight();
        // 这里想实现在targetW，targetH范围内实现等比缩放�?�如果不�?要等比缩�?   
        // 则将下面的if else语句注释即可   
        if (sx < sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
        if (type == BufferedImage.TYPE_CUSTOM) { // handmade   
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
                    targetH);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        } else
            target = new BufferedImage(targetW, targetH, type);
        Graphics2D g = target.createGraphics();
        // smoother than exlax:   
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }

    /**  
     * 实现图像的等比缩放和缩放后的截取  
     * @param inFilePath 要截取文件的路径  
     * @param outFilePath 截取后输出的路径  
     * @param width 要截取宽�?  
     * @param hight 要截取的高度  
     * @param proportion  
     * @throws Exception  
     */

    public static InputStream saveImageAsJpg(InputStream in,
            int width, int hight, boolean proportion) throws Exception {

      
        BufferedImage srcImage = ImageIO.read(in);
        if (width > 0 || hight > 0) {
            // 原图的大�?   
            int sw = srcImage.getWidth();
            int sh = srcImage.getHeight();
            // 如果原图像的大小小于要缩放的图像大小，直接将要缩放的图像复制过去   
            if (sw > width && sh > hight) {
                srcImage = resize(srcImage, width, hight);
                ByteArrayOutputStream os = new ByteArrayOutputStream();   
                ImageIO.write(srcImage, "jpg", os);   
            } else {
//                String fileName = saveFile.getName();
//                String formatName = fileName.substring(fileName
//                        .lastIndexOf('.') + 1);
//                ImageIO.write(srcImage, formatName, saveFile);
                return in;
            }
        }
        // 缩放后的图像的宽和高   
        int w = srcImage.getWidth();
        int h = srcImage.getHeight();
        // 如果缩放后的图像和要求的图像宽度�?样，就对缩放的图像的高度进行截取   
        if (w == width) {
            // 计算X轴坐�?   
            int x = 0;
            int y = h / 2 - hight / 2;
            return saveSubImage(srcImage, new Rectangle(x, y, width, hight));
        }
        // 否则如果是缩放后的图像的高度和要求的图像高度�?样，就对缩放后的图像的宽度进行截�?   
        else if (h == hight) {
            // 计算X轴坐�?   
            int x = w / 2 - width / 2;
            int y = 0;
            return saveSubImage(srcImage, new Rectangle(x, y, width, hight));
        }
        return null;
    }

    /**  
     * 实现缩放后的截图  
     * @param image 缩放后的图像  
     * @param subImageBounds 要截取的子图的范�?  
     * @param subImageFile 要保存的文件  
     * @throws IOException  
     */
    private static InputStream saveSubImage(BufferedImage image,
            Rectangle subImageBounds) throws IOException {
//        if (subImageBounds.x < 0 || subImageBounds.y < 0
//                || subImageBounds.width - subImageBounds.x > image.getWidth()
//                || subImageBounds.height - subImageBounds.y > image.getHeight()) {
//            
//            return;
//        }
        BufferedImage subImage = image.getSubimage(subImageBounds.x,
                subImageBounds.y, subImageBounds.width, subImageBounds.height);
//        String fileName = subImageFile.getName();
//        String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
//        ImageIO.write(subImage, formatName, subImageFile);
        
        ByteArrayOutputStream os = new ByteArrayOutputStream();   
        ImageIO.write(subImage, "jpg", os);   
        InputStream is = new ByteArrayInputStream(os.toByteArray());  
        return is ;
    }
      
    /** 
     * 程序入口：用于测试 
     * @param args 
     * @throws Exception 
     */ 
    @SuppressWarnings({ "static-access", "unused" })
    public static void main(String[] args) throws Exception { 
        // 1-缩放图像： 
//        // 方法一：按比例缩放 
    	File file = new File("F:/myfile/102498.jpg");
    	InputStream in;
    	ImageUtil util = new ImageUtil();
		try {
			in = new FileInputStream(file);
			InputStream imageInputSteam  = util.saveImageAsJpg(in, 400, 300, true);
			 ByteArrayOutputStream os = new ByteArrayOutputStream();   
		      
		      
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
        

//        // 方法二：按高度和宽度缩放 
//         ImageUtil.scale2("f:/test.jpg", "G:/新建文件夹/abc_test1.jpg", 800, 800, false); 
//         ImageUtil.scale2("f:/test.jpg", "G:/新建文件夹/abc_test2.jpg", 300, 300, false);//测试OK 
//        // 2-切割图像： 
//        // 方法一：按指定起点坐标和宽高切割 
//        // 方法二：指定切片的行数和列数 
//    	ImageUtil.cut("D:/1.jpg", "D:/1_cut.jpg", 121, 86, 55, 55);//测试OK 
//    	ImageUtil.cut("D:/11.png", "D:/11_cut.png", 0, 0, 200, 200 );//测试OK 
//        ImageUtil.cut2("e:/abc.jpg", "e:/", 2, 2 );//测试OK 
        // 方法三：指定切片的宽度和高度 
//        ImageUtil.cut3("f:/test.jpg", "G:/新建文件夹/", 100, 100 );//测试OK 
//    	String str = "Koala.jpg";
//    	int t = str.split("\\.").length;
//        System.out.println(str.split("\\.")[t-2]);
//        // 3-图像类型转换： 
//        ImageUtil.convert("e:/abc.jpg", "GIF", "e:/abc_convert.gif");//测试OK 
//        // 4-彩色转黑白： 
//        ImageUtil.gray("e:/abc.jpg", "e:/abc_gray.jpg");//测试OK 
//        // 5-给图片添加文字水印： 
//        // 方法一： 
//        ImageUtil.pressText("我是水印文字","e:/abc.jpg","e:/abc_pressText.jpg","宋体",Font.BOLD,Color.white,80, 0, 0, 0.5f);//测试OK 
//        // 方法二： 
//        ImageUtil.pressText2("我也是水印文字", "e:/abc.jpg","e:/abc_pressText2.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.5f);//测试OK 
//         
//        // 6-给图片添加图片水印： 
//        ImageUtil.pressImage("e:/abc2.jpg", "e:/abc.jpg","e:/abc_pressImage.jpg", 0, 0, 0.5f);//测试OK 
    } 
}
